previous page main page next page

Let's move now to the code for the Click event of the cmdResults button. This is a little more complicated...
 

Private Sub cmdResults_Click()

Dim Days As Integer
Dim TextOut As String

LFCR = Chr(10) + Chr(13)

Days = 365 * Age

'build output string
TextOut = "Hello " + UserName
TextOut = TextOut + LFCR
TextOut = TextOut + "You are at least "
TextOut = TextOut + Str(Days)
TextOut = TextOut + " days old!"

lblResults.Caption = TextOut

End Sub
 

We'll explain what all of this does in a moment.

  • enter the code and save your work
  • run the program and note what happens
     

OK, let's go through this code, starting with these two lines:
 

Dim Days As Integer
Dim TextOut As String

Nothing too mysterious here. We've declared Days as an integer and this is what we use for the person's age in days. We've also declared TextOut as a string and this is what we're going to use as the message to be printed out. But why didn't we declare them in the General Declarations section along with UserName and Age?

Well the answer is that we could have done and the program would still work. Notice, though, that Days and TextOut are only used in the cmdResults Click event and nowhere else. UserName and Age, on the other hand, are used in both the cmdDetails Click event and also the cmdResults Click event so it makes sense to declare them just once, right at the start. In general, it's good practice if a variable is only used in one procedure, or event, to just declare it where it's being used.
 

Continued...
 

previous page main page next page ©  ePublish Scotland  1999